package com.jcark.ui.view

import android.app.Activity
import android.content.Context
import android.os.Build
import android.text.TextUtils
import android.util.AttributeSet
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.View
import android.widget.RelativeLayout
import androidx.core.content.ContextCompat
import androidx.core.widget.TextViewCompat
import com.jcark.ui.R
import com.jcark.ui.databinding.ViewPageTitleBinding

class PageTitleView(context: Context?, attrs: AttributeSet?) : RelativeLayout(context, attrs) {
    val binding: ViewPageTitleBinding
    var backClick: (() -> Unit)? = null
    var rightClick: (() -> Unit)? = null

    init {
        LayoutInflater.from(context).inflate(R.layout.view_page_title, this)
        binding = ViewPageTitleBinding.bind(this)

        // 获取属性集合 TypedArray
        val typedArray = context!!.obtainStyledAttributes(attrs, R.styleable.PageTitleView)
        val textColor = typedArray!!.getColor(
            R.styleable.PageTitleView_text_color,
            ContextCompat.getColor(context, R.color.color_333333)
        )
        binding.mtvPageTitle.setTextColor(textColor)

        val rightTextColor = typedArray!!.getColor(
            R.styleable.PageTitleView_right_text_color,
            ContextCompat.getColor(context, R.color.color_333333)
        )
        binding.mtvPageTitleRight.setTextColor(rightTextColor)

        val leftImage =
            typedArray.getResourceId(R.styleable.PageTitleView_left_image, R.mipmap.ic_back_20)
        binding.ivPageTitleLeft.setImageResource(leftImage)

        //标题
        val title = typedArray.getString(R.styleable.PageTitleView_title)
        val right = typedArray.getString(R.styleable.PageTitleView_right_text)
        binding.mtvPageTitleRight.visibility =
            if (TextUtils.isEmpty(right)) View.GONE else View.VISIBLE
        binding.mtvPageTitleRight.text = right ?: ""

        binding.mtvPageTitle.text = title ?: ""

        val rightImage =
            typedArray.getResourceId(R.styleable.PageTitleView_right_image, -1)
        if (rightImage != -1) {
            binding.ivPageTitleRight.visibility = View.VISIBLE
            binding.ivPageTitleRight.setImageResource(rightImage)
        }

        binding.ivPageTitleLeft.setOnClickListener {
            if (backClick == null) {
                if (context is Activity) {
                    context.finish()
                }
            } else {
                backClick?.invoke()
            }
        }
        binding.mtvPageTitleRight.setOnClickListener {
            rightClick?.invoke()
        }
        binding.ivPageTitleRight.setOnClickListener {
            rightClick?.invoke()
        }
    }

    fun title(title: String) {
        binding.mtvPageTitle.text = title
    }

    fun leftImage(image: Int) {
        binding.ivPageTitleLeft.setImageResource(image)
    }

    fun textColor(color: Int) {
        binding.mtvPageTitle.setTextColor(color)
    }

    /**
     * 设置标题自动适应文字大小列表
     */
    fun setTitleTextSizeList(textSizeList: IntArray) {
        binding.mtvPageTitle.apply {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                setAutoSizeTextTypeUniformWithPresetSizes(textSizeList, TypedValue.COMPLEX_UNIT_SP)
            } else {
                TextViewCompat.setAutoSizeTextTypeUniformWithPresetSizes(
                    this,
                    textSizeList,
                    TypedValue.COMPLEX_UNIT_SP
                )
            }
        }
    }
}